//
//  CCBILoader.cpp
//  LeftRight
//
//  Created by 杨岑 on 7/11/14.
//
//

#include "CCBILoader.h"

Node* CCBILoader::load()
{
    auto ccbReader = new CCBReader(NodeLoaderLibrary::getInstance(), this, this, this);
    ccbReader->setCCBRootPath(_rootPath.c_str());
    
    NodePtr node;
    if (!_parentSize.width && !_parentSize.height)
    {
        node = ccbReader->readNodeGraphFromFile(_file.c_str(), _owner);
    }
    else
    {
        node = ccbReader->readNodeGraphFromFile(_file.c_str(), _owner, _parentSize);
    }
    ccbReader->release();
    
    return node;
}

void CCBILoader::onNodeLoaded(cocos2d::Node * pNode,  NodeLoader * pNodeLoader)
{
	
}

SEL_MenuHandler CCBILoader::onResolveCCBCCMenuItemSelector(Ref * pTarget, const char* pSelectorName)
{
    if (pSelectorName)
    {
        auto iter = _mapMenuHandlers.find(pSelectorName);
        if (iter != _mapMenuHandlers.end())
        {
            return iter->second;
        }

        CCLOG("CCBILoader::onResolveCCBCCMenuItemSelector: %s not mapped", pSelectorName);
}
	return nullptr;
}

Control::Handler CCBILoader::onResolveCCBCCControlSelector(Ref * pTarget, const char* pSelectorName)
{
    if (pSelectorName)
    {
        auto iter = _mapControlHandlers.find(pSelectorName);
        if (iter != _mapControlHandlers.end())
        {
            return iter->second;
        }

        CCLOG("CCBILoader::onResolveCCBCCControlSelector: %s not mapped", pSelectorName);
}
	return nullptr;
}

bool CCBILoader::onAssignCCBCustomProperty(cocos2d::Ref* target, const char* memberVariableName, const cocos2d::Value& value)
{
    if (memberVariableName)
    {
        auto iter = _mapProps.find(memberVariableName);
        if (iter != _mapProps.end())
        {
            iter->second = value;
            return true;
        }

        CCLOG("CCBILoader::onAssignCCBCustomProperty: %s not mapped", memberVariableName);
}
    return false;
}

SEL_CallFuncN CCBILoader::onResolveCCBCCCallFuncSelector(cocos2d::Ref * pTarget, const char* pSelectorName)
{
    if (pSelectorName)
    {
        auto iter = _mapCallFuncs.find(pSelectorName);
        if (iter != _mapCallFuncs.end())
        {
            return iter->second;
        }
        
        CCLOG("CCBILoader::onResolveCCBCCCallFuncSelector: %s not mapped", pSelectorName);
    }
    return nullptr;
}

bool CCBILoader::onAssignCCBMemberVariable(Ref* pTarget, const char* pMemberVariableName, Node* pNode)
{
    if (pMemberVariableName)
    {
        // Check array first
        auto iterNodeArray = _mapNodeArray.find(pMemberVariableName);
        if (iterNodeArray != _mapNodeArray.end())
        {
            auto nodeArrayTuple = iterNodeArray->second;
            auto nodeArray = std::get<0>(nodeArrayTuple);
            size_t capacity = std::get<1>(nodeArrayTuple);
            size_t& count = *std::get<2>(nodeArrayTuple);
            if (count < capacity)
            {
                nodeArray[count++] = pNode;
            }
            else
            {
                CCLOG("CCBI has too many nodes named %s", pMemberVariableName);
                CCASSERT(false, "Please revise your code.");
            }
            return true;
        }
        
        auto iter = _mapMemberVars.find(pMemberVariableName);
        if (iter != _mapMemberVars.end())
        {
            iter->second = pNode;
            return true;
        }
        
        CCLOG("CCBILoader::onAssignCCBMemberVariable: %s not mapped", pMemberVariableName);
    }
    return false;
}

